package com.example.petback.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.petback.entity.MsgEntity;
import com.example.petback.model.UserModel;
import com.example.petback.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/petback/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/getAllUser")
    public MsgEntity getAllUsers(){
        List<UserModel> users = userService.getAllUsers();
        return new MsgEntity("SUCCESS","200",users);
    }

    @PostMapping("/updatePasswordById")
    public MsgEntity updatePasswordById(@RequestBody JSONObject jsonObject){
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        userService.updatePasswordById(username,password);
        return new MsgEntity("SUCCESS","200",true);
    }

    @PostMapping("/updateStatusById")
    public MsgEntity updateStatusById(@RequestBody JSONObject jsonObject){
        String username = jsonObject.getString("username");
        userService.updateStatusById(username);
        return new MsgEntity("SUCCESS","200",true);
    }

    @PostMapping("/deleteUserById")
    public MsgEntity deleteUserById(@RequestBody JSONObject jsonObject){
        String username = jsonObject.getString("username");
        userService.deleteUserById(username);
        return new MsgEntity("SUCCESS","200",true);
    }
}
